//
//  main.m
//  05_遍历
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    
    NSArray *arr = @[@"哈哈", @"2", @"3", @"4", @"5"];
    NSMutableArray *mArr = [NSMutableArray array];
    for (NSInteger i = arr.count - 1; i >= 0; i--) {
        [mArr addObject:arr[i]];
    }
    NSLog(@"%@", mArr);
    
    // 不可变字典用字面量声明
    NSDictionary *dic = @{@"name":@"dengkai", @"sex":@"男", @"age":@16};
    NSLog(@"%@", dic);
    
    // 枚举器(遍历方式),NSEnumerator依赖集合类型,不能单独使用
    NSEnumerator *stringArrayEnumerator = [arr objectEnumerator];
    id value = nil;
    while (value = [stringArrayEnumerator nextObject]) {
        NSLog(@"value = %@", value);
    }
    
    // 用枚举器倒序数组
    NSEnumerator *reverse = [arr reverseObjectEnumerator];
    id reverseValue = nil;
    while (reverseValue = [reverse nextObject]) {
        NSLog(@"%@", reverseValue);
    }

    // 字典是无序的,没有倒序
    // 默认字典打印value, 改成[dic.allKeys objectEnumerator]打印key
    NSEnumerator *dicEnumetor = [dic objectEnumerator];
    id dicValue = nil;
    while (dicValue = [dicEnumetor nextObject]) {
//        NSArray *keys = [dic allKeysForObject:dicValue];
//        NSLog(@"把key取出来%@", keys);
        NSLog(@"%@", dicValue);
    }
    
    // 集合枚举
    NSSet *set = [NSSet setWithObjects:@"110", @"120", @"119", @"4555", @"255", nil];
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnumerator nextObject]) {
        NSLog(@"setValue = %@", setValue);
    }
    
    NSArray *arr1 = @[@"7", @"3", @"8", @"2", @"6"];
    NSString *string = [NSString string];
    for (NSString *str in arr1) {
        string = [string stringByAppendingString:str];
    }
    NSLog(@"%@", string);
    
    // 排序 NSSortDescriptor
    // 参数 key: 排序条件
    // 参数 ascending: 升序(YES),降序(NO)
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
    // 数组有排序的方法,参数为一个排序条件的数组
    // 不可变数组进行排序: sortedArrayUsingDescriptors
    arr1 = [arr1 sortedArrayUsingDescriptors:@[sort]];
    NSLog(@"排序之后:%@", arr1);
    
    // 可变数组进行排序: sortUsingDescriptors
    NSMutableArray *mutableArr = [NSMutableArray arrayWithObjects:@"7", @"3", @"8", @"2", @"6", nil];
    [mutableArr sortUsingDescriptors:@[sort]];
    NSLog(@"可变数组排序之后:%@", mutableArr);
    
    // 排序: 可变数组和不可变数组通过调用sort排序调用的方法不同
    // 1.不可变数组,排序后生成新的数组
    // 2.可变数组,排序后可以直接在本身上修改
    Person *p1 = [[Person alloc] initWithName:@"dengkai" age:45];
    Person *p2 = [[Person alloc] initWithName:@"yuhcenjing" age:56];
    Person *p3 = [[Person alloc] initWithName:@"liuhe" age:22];
    Person *p4 = [[Person alloc] initWithName:@"mengjie" age:67];
    Person *p5 = [[Person alloc] initWithName:@"shixiaoqi" age:34];
    
    NSArray *personArr = @[p1, p2, p3, p4, p5];
    NSLog(@"排序前:%@", personArr);
    NSSortDescriptor *perSortName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSSortDescriptor *perSortAge = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    personArr = [personArr sortedArrayUsingDescriptors:@[perSortAge]];
    NSLog(@"排序后:%@", personArr);
    
    // 排序: selector
    NSArray *sortString = @[@"dengkai", @"yuhcenjing", @"liuhe", @"mengjie", @"shixiaoqi"];
    sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@", sortString);    
    
    return 0;
}
